package com.txzhang.springcloud.storageservice.controller;

import com.txzhang.springcloud.storageservice.service.AccountService;
import com.txzhang.springcloud.storageservice.service.GoodService;
import com.txzhang.springcloud.storageservice.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StorageController {

    @Autowired
    private AccountService accountService;
    @Autowired
    private GoodService goodService;
    @Autowired
    private OrderService orderService;
    /**
     * 购买商品
     * @param userId
     * @param goodCode
     * @param count
     * @return
     */
    @GlobalTransactional(rollbackFor = RuntimeException.class)
    @RequestMapping("/buyGood")
    public Object buyGood(Integer userId, String goodCode, Integer count, Integer price){
        //创建订单
        orderService.createOrder(userId, goodCode, count);
        //减少库存
        goodService.reduce(goodCode, count);
        //扣除对应账户的钱
        accountService.deduction(userId, price * count);
        return "购买成功！";
    }
}
